import { MagicWeaponBase } from "./MagicWeaponBase";
import { GImageBitmap } from "../../../../com/mui/controls/GImageBitmap";
import { ImagesConst } from "../../resource/ImagesConst";
import { ResolveItemRenderer } from "./renderer/ResolveItemRenderer";
import { ObjCreate } from "../common/ObjCreate";
import { Language } from "../../../../extend/language/Language";
import { GlobalStyle } from "../../../common/global/GlobalStyle";
import { LoaderHelp } from "../../../common/display/LoaderHelp";
import { ResFileConst } from "../../resource/ResFileConst";
import { Dispatcher } from "../../../mvc/core/Dispatcher";
import { DataEvent } from "../../events/DataEvent";
import { EventName } from "../../mvc/EventName";
import { GlobalClass } from "../../../../com/mui/core/GlobalClass";
import { MagicWeaponItemData } from "./MagicWeaponItemData";
import { ItemData } from "../../resource/info/ItemData";
import { DataProvider } from "../../../../fl/data/DataProvider";
import { ComposeAmuletVo } from "./data/ComposeAmuletVo";
import { Alert } from "../../../../com/mui/controls/Alert";
import { PulseSharedObject } from "../player/PulseSharedObject";
import { Cache } from "../../cache/Cache";
import { MsgManager } from "../../manager/MsgManager";
import { CheckBoxWin } from "../common/alertwins/CheckBoxWin";
import { MagicWeaponUtil } from "./MagicWeaponUtil";
import { ListEvent } from "../../../../fl/events/ListEvent";
import { ItemConfig } from "../../resource/ItemConfig";
import { ItemExInfo } from "../../resource/info/ItemExInfo";
import { GButton } from "../../../../com/mui/controls/GButton";
import { GTextFiled } from "../../../../com/mui/controls/GTextFiled";
type int = number;
//class MagicWeaponCompositePanel
    
    export  class MagicWeaponCompositePanel extends MagicWeaponBase
    {
        constructor()
        {
            super();
            this.init();
            return;
        }

        /* internal  */init(): void
        {
            var loc1=new GImageBitmap(ImagesConst.MagicWeaponBg, 43, 188);
            this.addChild(loc1);
            this._materialItemOne = new ResolveItemRenderer(false);
            this._materialItemOne.addEventListener(flash.events.MouseEvent.CLICK, this.onPutDownMaterialItemOne);
            ObjCreate.setObjAttri(this._materialItemOne, 71, 175, -1, -1, this);
            this._materialItemTwo = new ResolveItemRenderer(false);
            this._materialItemTwo.addEventListener(flash.events.MouseEvent.CLICK, this.onPutDownMaterialItemTwo);
            ObjCreate.setObjAttri(this._materialItemTwo, 287, 175, -1, -1, this);
            this._productItem = new ResolveItemRenderer(false);
            ObjCreate.setObjAttri(this._productItem, 184, 300, -1, -1, this);
            this._compositeBtn = ObjCreate.createGButton(Language.getString(27716), 130, 416, 72, 26, this, "ButtonRed");
            this._compositeBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCompositeHandler);
            this._compositeAllBtn = ObjCreate.createGButton(Language.getString(27717), 232, 416, 72, 26, this, "ButtonRed");
            this._compositeAllBtn.addEventListener(flash.events.MouseEvent.CLICK, this.onCompositeAllandler);
            ObjCreate.createGTextField(Language.getString(27666), 304, 421, 80, 20, this, GlobalStyle.textFormat2);
            ObjCreate.createTextBox("1000", 338, 420, 70, 20, this, GlobalStyle.textFormatWhite);
            ObjCreate.createBitmap(ImagesConst.Tongqian, 389, 421, this);
            ObjCreate.createBg(31, 452, 380, 2, this, ImagesConst.SplitLine);
            ObjCreate.createFireTip(Language.getString(27718), 40, 455, this, true);
            var loc2=ObjCreate.createImgBtn(ImagesConst.RuleExplain, 350, 459, 60, 23, this, "Button");
            loc2.gbutton.toolTipData = Language.getString(27760);
            loc2.gbutton.addEventListener(flash.events.MouseEvent.CLICK, this.clickHelpHandler);
            this._txt = ObjCreate.createGTextField(Language.getString(27759), 203, 336, 80, 40, this, GlobalStyle.setTextFormatLeading(GlobalStyle._textFormat1, 1));
            this.addEventListener(flash.events.Event.REMOVED_FROM_STAGE, this.clean);
            LoaderHelp.addResCallBack(ResFileConst.magicWeapon, this.onLoadResHandler);
            return;
        }

        /* internal  */clickHelpHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponShowPageGuide));
            return;
        }

        /* internal  */onLoadResHandler(): void
        {
            var loc1=GlobalClass.getBitmap(ImagesConst.MagicWeaponComposLeftTxt);
            ObjCreate.setObjAttri(loc1, 59, 180, -1, -1, this);
            var loc2=GlobalClass.getBitmap(ImagesConst.MagicWeaponComposRightTxt);
            ObjCreate.setObjAttri(loc2, 278, 180, -1, -1, this);
            var loc3=GlobalClass.getBitmap(ImagesConst.MagicWeaponComposBg);
            ObjCreate.setObjAttri(loc3, 92, 243, -1, -1, this);
            var loc4=GlobalClass.getBitmap(ImagesConst.MagicWeaponComposBottomTxt);
            ObjCreate.setObjAttri(loc4, 35, 383, -1, -1, this);
            return;
        }

        public clean(arg1: Object /* flash.events.Event */=null): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (!(loc1 && loc1.isSelect)) 
                {
                    continue;
                }
                loc1.isSelect = false;
            }
            operateMaterialData = [];
            this.onPutDownMaterialItemOne();
            this.onPutDownMaterialItemTwo();
            if (arg1 && pageSelecter) 
            {
                restPage();
            }
            return;
        }

        /* internal  */onPutDownMaterialItemTwo(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._materialItemTwo.data = new MagicWeaponItemData(null);
            this._productItem.data = new MagicWeaponItemData(null);
            this._txt.visible = true;
            if (arg1) 
            {
                this.setSelect();
            }
            return;
        }

        /* internal  */onPutDownMaterialItemOne(arg1: Object /* flash.events.MouseEvent */=null): void
        {
            this._materialItemOne.data = new MagicWeaponItemData(null);
            this._materialItemTwo.data = new MagicWeaponItemData(null);
            this._productItem.data = new MagicWeaponItemData(null);
            this._txt.visible = true;
            this._filterMaterialData = _materialData.filter(this.compositeItemFilterFun);
            this.drawList = this._filterMaterialData;
            if (arg1) 
            {
                this.setSelect();
            }
            return;
        }

        /* internal  */getUId(arg1): string
        {
            var loc1=null;
            if (arg1 && arg1 is ResolveItemRenderer && arg1.data) 
            {
                loc1 = arg1.data as MagicWeaponItemData;
                if (loc1 && loc1.itemData) 
                {
                    return loc1.itemData.uid;
                }
            }
            else if (arg1 && arg1 is ItemData) 
            {
                return arg1.uid;
            }
            return "";
        }

        /* internal  */setSelect(): void
        {
            var loc1=null;
            var loc2=0;
            var loc3=_materialData;
            for(loc1 of loc3) 
            {
                if (loc1 && loc1.itemData && (loc1.itemData.uid == this.getUId(this._materialItemOne) || loc1.itemData.uid == this.getUId(this._materialItemTwo.itemData))) 
                {
                    loc1.isSelect = true;
                    continue;
                }
                loc1.isSelect = false;
            }
            this.drawList = this._filterMaterialData;
            return;
        }

        /* internal  */set drawList(arg1: Array<any>)
        {
            _materialList.dataProvider = new DataProvider(arg1);
            _materialList.drawNow();
            if (pageSelecter) 
            {
                pageSelecter.maxPage = int(Math.ceil(arg1.length / 6));
                currentPage();
            }
            return;
        }

        /* internal  */onCompositeHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var onBtnClickHandler: Function;
            var vo: ComposeAmuletVo;
            var isNeedAlert: Boolean;

            var loc1;
            onBtnClickHandler = null;
            vo = null;
            isNeedAlert = false;
            e = arg1;
            onBtnClickHandler = (arg1: int, arg2: Boolean): void
            {
                if (arg1 == Alert.OK) 
                {
                    Alert.resetToDefault();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponComposite, vo));
                }
                if (arg2) 
                {
                    PulseSharedObject.save("CompositeMagicWeaponSingle", true);
                }
                return;
            }
            if (this._materialItemOne.itemData && this._materialItemTwo.itemData) 
            {
                if (Cache.instance.role.money.coin + Cache.instance.role.money.coinBind < 1000) 
                {
                    MsgManager.showRollTipsMsg(Language.getString(28001));
                    return;
                }
                vo = new ComposeAmuletVo();
                vo.amuletUid.push(this._materialItemOne.itemData.uid);
                vo.amuletUid.push(this._materialItemTwo.itemData.uid);
                isNeedAlert = !PulseSharedObject.isTodayNotTips("CompositeMagicWeaponSingle");
                if (isNeedAlert) 
                {
                    Alert.alertWinRenderer = CheckBoxWin;
                    Alert.show(Language.getString(27628), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                    Alert.okLabel = Language.getString(27629);
                    return;
                }
                Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponComposite, vo));
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(27714));
            }
            return;
        }

        /* internal  */onCompositeAllandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            var e: Object /* flash.events.MouseEvent */;
            var arr: Array<any>;
            var vo: ComposeAmuletVo;
            var item: MagicWeaponItemData;
            var isNeedAlert: Boolean;
            var onBtnClickHandler: Function;

            var loc1;
            vo = null;
            item = null;
            onBtnClickHandler = null;
            e = arg1;
            onBtnClickHandler = (arg1: int, arg2: Boolean): void
            {
                if (arg1 == Alert.OK) 
                {
                    Alert.resetToDefault();
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponComposite, vo));
                }
                if (arg2) 
                {
                    PulseSharedObject.save("CompositeMagicWeaponAll", true);
                }
                return;
            }
            arr = _materialData.filter(this.compositeItemFilterFun);
            if (arr && arr.length <= 0) 
            {
                return;
            }
            vo = new ComposeAmuletVo();
            var loc2=0;
            var loc3=arr;
            for(item of loc3) 
            {
                vo.amuletUid.push(item.itemData.uid);
            }
            if (vo.amuletUid.length < 2) 
            {
                MsgManager.showRollTipsMsg(Language.getString(27714));
                return;
            }
            if (Cache.instance.role.money.coin + Cache.instance.role.money.coinBind < 1000) 
            {
                MsgManager.showRollTipsMsg(Language.getString(28001));
            }
            isNeedAlert = !PulseSharedObject.isTodayNotTips("CompositeMagicWeaponAll");
            if (isNeedAlert) 
            {
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.show(Language.getString(27719), null, Alert.OK | Alert.CANCEL, null, onBtnClickHandler);
                Alert.okLabel = Language.getString(27629);
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.MagicWeaponComposite, vo));
            return;
        }

        /* internal  */compositeItemFilterFun(arg1, arg2: int, arg3: Array<any>): Boolean
        {
            var loc1;
            return (loc1 = arg1 as MagicWeaponItemData).itemData.color < 6 && loc1.itemData.magicWeaponLv <= 0;
        }

        /* internal  */compositeMaterialItemFilterFun(arg1, arg2: int, arg3: Array<any>): Boolean
        {
            var loc1=MagicWeaponItemData(arg1).itemData;
            var loc2;
            if ((loc2 = this._materialItemOne.itemData) == null) 
            {
                return false;
            }
            return loc2.type == loc1.type && loc2.color == loc1.color && loc1.magicWeaponLv <= 0;
        }

        public /* override */ updateWeaponInfo(): void
        {
            var loc2=null;
            var loc1=Cache.instance.magicWeapon.hasIdentifyAndNoWear;
            loc1.sort(MagicWeaponUtil.sortData);
            var loc3=0;
            var loc4=loc1;
            for(loc2 of loc4) 
            {
                if (loc2.itemData.uid == this.getUId(this._materialItemOne)) 
                {
                    this._materialItemOne.data = loc2;
                    loc2.isSelect = true;
                }
                if (loc2.itemData.uid != this.getUId(this._materialItemTwo.itemData)) 
                {
                    continue;
                }
                loc2.isSelect = true;
            }
            if (this._materialItemOne.itemData) 
            {
                this._filterMaterialData = loc1.filter(this.compositeMaterialItemFilterFun);
            }
            else 
            {
                this._filterMaterialData = loc1.filter(this.compositeItemFilterFun);
            }
            if (_materialList && loc1 && this._filterMaterialData) 
            {
                _materialData = loc1;
                this.drawList = this._filterMaterialData;
            }
            return;
        }

        protected /* override */ onEquipmentPageChange(arg1: Object /* flash.events.Event */): void
        {
            var loc1=_materiaPageSelecter.currentPage;
            var loc2=(loc1 - 1) * 6;
            var loc3=this._filterMaterialData.length - loc2 > 6 ? 6 : this._filterMaterialData.length - loc2;
            var loc4=[];
            var loc5=loc2;
            while (loc5 < loc2 + loc3) 
            {
                loc4[loc5 - loc2] = this._filterMaterialData[loc5];
                ++loc5;
            }
            _materialList.dataProvider = new DataProvider(loc4);
            _materialList.drawNow();
            return;
        }

        protected /* override */ onSelectItemHandler(arg1: ListEvent): void
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=arg1.item as MagicWeaponItemData;
            if (loc1) 
            {
                if (this._materialItemOne.itemData != null) 
                {
                    if (this._materialItemOne.itemData && !(this._materialItemOne.itemData.uid == loc1.itemData.uid)) 
                    {
                        this._materialItemTwo.data = loc1;
                        loc2 = ItemConfig.instance.getItemInfoByParams(loc1.itemData.category, loc1.itemData.type, 2, -1, loc1.itemData.color + 1);
                        loc3 = new ItemData(loc2.item.code);
                        (loc4 = new ItemExInfo()).al_ify_flg = 1;
                        loc3.itemExInfo = loc4;
                        this._productItem.data = new MagicWeaponItemData(loc3);
                        this._txt.visible = false;
                    }
                }
                else 
                {
                    this._materialItemOne.data = loc1;
                    this._filterMaterialData = _materialData.filter(this.compositeMaterialItemFilterFun);
                    this.drawList = this._filterMaterialData;
                }
                this.setSelect();
            }
            return;
        }

        /* internal */ /* var */_filterMaterialData: Array<any>;

        /* internal */ /* var */_compositeBtn: GButton;

        /* internal */ /* var */_compositeAllBtn: GButton;

        /* internal */ /* var */_materialItemOne: ResolveItemRenderer;

        /* internal */ /* var */_materialItemTwo: ResolveItemRenderer;

        /* internal */ /* var */_productItem: ResolveItemRenderer;

        /* internal */ /* var */_txt: GTextFiled;
    }
